package org.apselex.model;

/* Copyright (c) 2008 Evan Williams

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
 */

import java.util.ArrayList;

public class ModelTag {

	private int iTagId;

	private int iParentId;

	private String sTagName;

	private String sTagDotted;

	private String sType;

	private ArrayList<ModelTag> children = null;

	/**
	 * @param tagId
	 * @param tagName
	 */
	public ModelTag(int tagId, String tagName) {
		iTagId = tagId;
		sTagName = tagName;
		iParentId = 0;
		setChildren(new ArrayList<ModelTag>());
		sTagDotted = "";
		sType = "";
	}

	public int getTagId() {
		return iTagId;
	}

	public void setTagId(int tagId) {
		iTagId = tagId;
	}

	public String getTagName() {
		return sTagName;
	}

	public void setTagName(String tagName) {
		sTagName = tagName;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "[" + iTagId + ":" + sTagName + "]";
	}

	public boolean add(ModelTag e) {
		return getChildren().add(e);
	}

	public int getParentId() {
		return iParentId;
	}

	public void setParentId(int parentId) {
		iParentId = parentId;
	}

	public void printTree(ModelTag root) {

		printSubTree(root, 1);

	}

	private void printSubTree(ModelTag root, int iLevel) {

		String sSpaces = "         ";
		String sIndent = sSpaces.substring(0, iLevel);

		System.out.println(iLevel + sIndent + root.getTagName() + "> " + root.getTagDotted());
		int iNext = iLevel + 1;
		for (ModelTag m : root.getChildren()) {
			printSubTree(m, iNext);
		}
	}

	public void applySubTree(ModelTag root) {

		applySubTree(root, 1, "");
	}

	private void applySubTree(ModelTag root, int iLevel, String sHistory) {

		String sSpaces = "         ";
		String sIndent = sSpaces.substring(0, iLevel);

		String sNew = "";
		if (root.getType().startsWith("user_")) {
			if (sHistory.length() > 0) {
				sNew = sHistory + "." + root.sTagName;
			} else {
				sNew = root.sTagName;
			}
			root.setTagDotted(sNew);
		} 
//		else {
//			sNew = sHistory + "." + root.sTagName;
//		}

		System.out.println(iLevel + sIndent + root.getTagDotted());
		int iNext = iLevel + 1;
		for (ModelTag m : root.getChildren()) {
			applySubTree(m, iNext, sNew);
		}
	}

	public String getTagDotted() {
		return sTagDotted;
	}

	public void setTagDotted(String tagDotted) {
		sTagDotted = tagDotted;
	}

	public String getType() {
		return sType;
	}

	public void setType(String type) {
		sType = type;
	}

	public void setChildren(ArrayList<ModelTag> children) {
		this.children = children;
	}

	public ArrayList<ModelTag> getChildren() {
		return children;
	}

}
